//
//  UIPopoverListView.m
//  UIPopoverListViewDemo
//
//  Created by su xinde on 13-3-13.
//  Copyright (c) 2013年 su xinde. All rights reserved.
//

#import "UIPopoverListView.h"
#import <QuartzCore/QuartzCore.h>

//#define FRAME_X_INSET 20.0f
//#define FRAME_Y_INSET 40.0f

@interface UIPopoverListView ()

- (void)defalutInit;
- (void)fadeIn;
- (void)fadeOut;

@end

@implementation UIPopoverListView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self defalutInit];
    }
    return self;
}

- (void)dealloc{
    _titleView=nil;
    _overlayView=nil;
    
    if (self.listView) {
        self.listView.dataSource=nil;
        self.listView.delegate=nil;
        self.listView=nil;
    }
    
    self.datasource=nil;
    self.delegate=nil;
}

- (void)defalutInit
{
    self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    self.layer.borderWidth = 1.0f;
    self.layer.cornerRadius = 10.0f;
    self.clipsToBounds = TRUE;
    self.backgroundColor=[UIColor whiteColor];
    _titleView = [[UILabel alloc] initWithFrame:CGRectZero];
    _titleView.font = [UIFont boldSystemFontOfSize:16];
    _titleView.backgroundColor = [UIColor whiteColor];
    _titleView.numberOfLines=2;
    _titleView.textAlignment = NSTextAlignmentCenter;
    _titleView.textColor = [UIColor orangeColor];
    CGFloat xWidth = self.bounds.size.width;
    _titleView.lineBreakMode = NSLineBreakByTruncatingTail;
    _titleView.frame = CGRectMake(0, 0, xWidth, 50.0f);
    
    [self addSubview:_titleView];
    
    _Textfield = [[UITextField alloc]initWithFrame:CGRectZero];
    _Textfield.font = [UIFont boldSystemFontOfSize:16];
    _Textfield.borderStyle=UITextBorderStyleRoundedRect;
    _Textfield.backgroundColor = [UIColor whiteColor];
    _Textfield.textAlignment = NSTextAlignmentLeft;
    _Textfield.textColor = [UIColor orangeColor];
    CGFloat TWidth = self.bounds.size.width;
    _Textfield.placeholder=@"請輸入驗證碼";
    _Textfield.frame = CGRectMake(10, 55, TWidth-140, 40.0f);
    
    [self addSubview:_Textfield];
    
    _ReBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _ReBtn.layer.borderColor=[[UIColor grayColor]CGColor];
    _ReBtn.layer.borderWidth=1;
    _ReBtn.layer.cornerRadius=3;
    [_ReBtn setFrame:CGRectMake(TWidth-110, 55, 90, 40)];
    [_ReBtn addTarget:self action:@selector(RebtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_ReBtn];

    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50-1, xWidth, 1.0)];
    line.backgroundColor = [UIColor orangeColor];
    [line setContentMode:UIViewContentModeScaleToFill];
    [self addSubview:line];
    
    CGRect tableFrame = CGRectMake(0, 100.0f, xWidth, self.bounds.size.height-100.0f);
    _listView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
    _listView.dataSource = self;
    _listView.delegate = self;
    _listView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self addSubview:_listView];
    
    _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
//    [_overlayView addTarget:self
//                     action:@selector(Cancel)
//           forControlEvents:UIControlEventTouchUpInside];
    
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.datasource &&
       [self.datasource respondsToSelector:@selector(popoverListView:numberOfRowsInSection:)])
    {
        return [self.datasource popoverListView:self numberOfRowsInSection:section];
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.datasource &&
       [self.datasource respondsToSelector:@selector(popoverListView:cellForIndexPath:)])
    {
        return [self.datasource popoverListView:self cellForIndexPath:indexPath];
    }
    return nil;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.delegate &&
       [self.delegate respondsToSelector:@selector(popoverListView:heightForRowAtIndexPath:)])
    {
        return [self.delegate popoverListView:self heightForRowAtIndexPath:indexPath];
    }
    
    return 0.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.delegate &&
       [self.delegate respondsToSelector:@selector(popoverListView:didSelectIndexPath:)])
    {
        [self.delegate popoverListView:self didSelectIndexPath:_Textfield.text];
    }
    
    [self dismiss];
}


#pragma mark - animations

- (void)fadeIn
{
    self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.alpha = 0;
    [UIView animateWithDuration:.35 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
    
}
-(void)RebtnAction
{
    if(_delegate && [_delegate respondsToSelector:@selector(ReBtn)]){
        [_delegate ReBtn];
    }
}
- (void)fadeOut
{
    [UIView animateWithDuration:.35 animations:^{
        self.transform = CGAffineTransformMakeScale(1.3, 1.3);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [_overlayView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];
}
- (void)setBtnTitle:(NSString *)title :(BOOL)isEnable
{
    [_ReBtn setTitle:title forState:UIControlStateNormal];
    [_ReBtn setEnabled:isEnable];
    _ReBtn.layer.borderColor=isEnable?[[UIColor orangeColor]CGColor]:[[UIColor grayColor]CGColor];
    [_ReBtn setTitleColor:isEnable?[UIColor orangeColor]:[UIColor grayColor] forState:UIControlStateNormal];
}
- (void)setTitle:(NSString *)title
{
    _titleView.text = title;
}

- (void)show
{
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:_overlayView];
    [keywindow addSubview:self];
    
    self.center = CGPointMake(keywindow.bounds.size.width/2.0f,
                              keywindow.bounds.size.height/2.0f);
    [self fadeIn];
}

- (void)dismiss
{
    [self fadeOut];
}

- (void)Cancel{
    // tell the delegate the cancellation
    if (self.delegate && [self.delegate respondsToSelector:@selector(popoverListViewCancel:)]) {
        [self.delegate popoverListViewCancel:self];
    }
    
    // dismiss self
    [self dismiss];
}

#define mark - UITouch
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//    [self Cancel];
}

@end
